package collection.queue.block_queue;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;

/**
 * @describe: 类描述信息
 * @author: morningcat.zhang
 * @date: 2019/4/16 7:20 PM
 */
public class PriorityBlockingQueueTest {

    /**
     * @see PriorityBlockingQueue
     */
    @Test
    public void test1() {
        PriorityBlockingQueue queue = new PriorityBlockingQueue();

        PriorityBlockingQueue queue2 = new PriorityBlockingQueue(16);

        /**
         * 根据指定的比较器对其元素进行排序
         */
        PriorityBlockingQueue queue3 = new PriorityBlockingQueue(16, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                return 0;
            }
        });
    }

    private void add(BlockingQueue queue){
        queue.offer("dffg");
        queue.offer("adbg");
        queue.offer("xcff");
    }

    @Test
    public void test2() {
        PriorityBlockingQueue<String> queue = new PriorityBlockingQueue();

        add(queue);

        /**
         * 获取但不移除此队列的头；如果此队列为空，则返回 null。
         */
        System.out.println(queue.peek());
        System.out.println(queue.peek());
        System.out.println(queue.peek());
        System.out.println(queue.peek());
    }

    @Test
    public void test3() {
        PriorityBlockingQueue<String> queue = new PriorityBlockingQueue();

        add(queue);

        /**
         * 获取并移除此队列的头，如果此队列为空，则返回 null。
         */
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
    }

    @Test
    public void test4() throws InterruptedException {
        PriorityBlockingQueue<String> queue = new PriorityBlockingQueue();

        add(queue);

        System.out.println(queue.take());
        System.out.println(queue.take());
        System.out.println(queue.take());
        // 发生阻塞
        System.out.println(queue.take());
    }

    @Test
    public void test5() {
        PriorityBlockingQueue<String> queue = new PriorityBlockingQueue();

        add(queue);

        List<String> list = new ArrayList();
        // 移除此队列中所有可用的元素，并将它们添加到给定 collection 中。
        queue.drainTo(list);
        System.out.println(list);
    }

    @Test
    public void test6() {
        PriorityBlockingQueue<String> queue = new PriorityBlockingQueue();

        add(queue);

        List<String> list = new ArrayList();
        queue.drainTo(list, 3);
        System.out.println(list);
    }

    @Test
    public void test7(){

    }

}
